package com.liuceng.zen.web.auth.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Pagination {
  /** "页码"参数名称 */
  private static final String PAGE_NUMBER_PARAMETER_NAME = "pageNumber";

  /** "总页数"参数名称 */
  private static final String TOTAL_PAGES_PARAMETER_NAME = "totalPages";

  /** "中间段页码数"参数名称 */
  private static final String SEGMENT_COUNT_PARAMETER_NAME = "segmentCount";

  /** "页码"变量名称 */
  private static final String PAGE_NUMBER_VARIABLE_NAME = "pageNumber";

  /** "总页数"变量名称 */
  private static final String PAGE_COUNT_VARIABLE_NAME = "totalPages";

  /** "中间段页码数"变量名称 */
  private static final String SEGMENT_COUNT_VARIABLE_NAME = "segmentCount";

  /** "是否存在上一页"变量名称 */
  private static final String HAS_PREVIOUS_VARIABLE_NAME = "hasPrevious";

  /** "是否存在下一页"变量名称 */
  private static final String HAS_NEXT_VARIABLE_NAME = "hasNext";

  /** "是否为首页"变量名称 */
  private static final String IS_FIRST_VARIABLE_NAME = "isFirst";

  /** "是否为末页"变量名称 */
  private static final String IS_LAST_VARIABLE_NAME = "isLast";

  /** "上一页页码"变量名称 */
  private static final String PREVIOUS_PAGE_NUMBER_VARIABLE_NAME = "previousPageNumber";

  /** "下一页页码"变量名称 */
  private static final String NEXT_PAGE_NUMBER_VARIABLE_NAME = "nextPageNumber";

  /** "首页页码"变量名称 */
  private static final String FIRST_PAGE_NUMBER_VARIABLE_NAME = "firstPageNumber";

  /** "末页页码"变量名称 */
  private static final String LAST_PAGE_NUMBER_VARIABLE_NAME = "lastPageNumber";

  /** "中间段页码"变量名称 */
  private static final String SEGMENT_VARIABLE_NAME = "segment";

  public Pagination(Integer pageNumber, Integer totalPages) {
    init(pageNumber, totalPages);
  }

  public void init(Integer pageNumber2, Integer totalPages2) {
    // 当前页
    Integer pageNumber = pageNumber2;
    // 总页数
    Integer totalPages = totalPages2;
    // 中间页码
    Integer segmentCount = 5;

    if (pageNumber == null || pageNumber < 1) {
      pageNumber = 1;
    }
    if (totalPages == null || totalPages < 1) {
      totalPages = 1;
    }
    if (segmentCount == null || segmentCount < 1) {
      segmentCount = 5;
    }
    // 是否存在上一页
    boolean hasPrevious = pageNumber > 1;
    // 是否存在下一页
    boolean hasNext = pageNumber < totalPages;
    // 是否存在第一页
    boolean isFirst = pageNumber == 1;
    // 是否存在最后一页
    boolean isLast = pageNumber.equals(totalPages);
    // 上一页
    int previousPageNumber = pageNumber - 1;
    // 下一页
    int nextPageNumber = pageNumber + 1;
    // 首页
    int firstPageNumber = 1;
    // 最后一页
    int lastPageNumber = totalPages;

    int startSegmentPageNumber = pageNumber - (int) Math.floor((segmentCount - 1) / 2D);
    int endSegmentPageNumber = pageNumber + (int) Math.ceil((segmentCount - 1) / 2D);
    if (startSegmentPageNumber < 1) {
      startSegmentPageNumber = 1;
    }
    if (endSegmentPageNumber > totalPages) {
      endSegmentPageNumber = totalPages;
    }
    List<Integer> segment = new ArrayList<Integer>();
    for (int i = startSegmentPageNumber; i <= endSegmentPageNumber; i++) {
      segment.add(i);
    }

    Map<String, Object> variables = new HashMap<String, Object>();
    variables.put(PAGE_NUMBER_VARIABLE_NAME, pageNumber);
    variables.put(PAGE_COUNT_VARIABLE_NAME, totalPages);
    variables.put(SEGMENT_COUNT_VARIABLE_NAME, segmentCount);
    variables.put(HAS_PREVIOUS_VARIABLE_NAME, hasPrevious);
    variables.put(HAS_NEXT_VARIABLE_NAME, hasNext);
    variables.put(IS_FIRST_VARIABLE_NAME, isFirst);
    variables.put(IS_LAST_VARIABLE_NAME, isLast);
    variables.put(PREVIOUS_PAGE_NUMBER_VARIABLE_NAME, previousPageNumber);
    variables.put(NEXT_PAGE_NUMBER_VARIABLE_NAME, nextPageNumber);
    variables.put(FIRST_PAGE_NUMBER_VARIABLE_NAME, firstPageNumber);
    variables.put(LAST_PAGE_NUMBER_VARIABLE_NAME, lastPageNumber);
    variables.put(SEGMENT_VARIABLE_NAME, segment);
  }
}
